home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13813 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: HOPPER.ACM.ORG!news
  2. From: varnk@e62.diebold.com (Ken Varn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Formatted print with variable output field width.
  5. Date: 10 Apr 1996 12:42:51 GMT
  6. Organization: Diebold
  7. Message-ID: <4kgacb$r3s@HOPPER.ACM.ORG>
  8. References: <berlinerDpMBoE.AEn@netcom.com>
  9. NNTP-Posting-Host: 199.218.232.47
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <berlinerDpMBoE.AEn@netcom.com>, berliner@netcom.com says...
  15. />
  16. />Does anyone know of a way of creating a variable output field width?
  17. />Say, for example, I wish to have a width of v+3, where v is some
  18. />integer, the result of some calculation. Is there some way to say
  19. />something like
  20. />
  21. />      printf("%<v+3>.<v>f", floatingnum);
  22. />
  23. />where the result of the evaluation of v+3 and of v will be
  24. />fed to printf?
  25.  
  26. All of the printf family of functions allow you to send arguments to format
  27. specifiers using the asterisk (*).  For example if you wanted to format a 
  28. float to have variable decimal places and width, then use the expression
  29.  
  30.     width = 5;
  31.     precision = 3;
  32.     printf("%*.*f",width,precision,floatingnum);
  33.  
  34. Note that when using numerical output instructions, the width specifier is 
  35. only a minimum width.  If the floating point number exceeds the width, it 
  36. will print the number in its entirety.  This could potentially be dangerous 
  37. if using sprintf.
  38.  
  39.